package com.atguigu.spzx.user.controller;

import com.atguigu.spzx.model.dto.user.UserRegistDto;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.user.UserInfoVo;
import com.atguigu.spzx.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(name = "会员模块")
@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoController {

    @Resource
    UserInfoService userInfoService;
    // 以后会员端 请求路径中满足 /api/**/auth/**格式 我们认为请求头中必须包含有效的token才可以继续执行 否则拒绝请求
    @Operation(summary = "获取用户信息")
    @GetMapping("/auth/getCurrentUserInfo")
    public Result getCurrentUserInfo(@RequestHeader(value = "token")
                                     String token){
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        return Result.ok(userInfoVo);
    }
    ///api/user/userInfo/logout
    @Operation(summary = "注销")
    @GetMapping("/logout")
    public Result logout(){
        userInfoService.logout();
        return Result.ok(null);
    }
    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserInfo userInfo){
        //登录成功 将登录状态保存到redis中  ，并返回redis数据的key
        String token = userInfoService.login(userInfo);
        return Result.ok(token);
    }
    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegistDto userRegistDto){
        userInfoService.register(userRegistDto);
        return Result.ok(null);
    }
}
